//
//  LEAlipayView.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/16.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "LEAlipayView.h"
@interface LEAlipayView()
@property(nonatomic,strong) UISegmentedControl *segment;
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UILabel *text1;
@property(nonatomic,strong) UILabel *text2;
@property(nonatomic,strong) UITextField *textField;
@property(nonatomic,strong) UIButton *okBtn;
@property(nonatomic,strong) UIButton *cancelBtn;
@property(nonatomic,assign) BOOL isMove;
@property(nonatomic,assign) long money;
@property(nonatomic,assign) long aliMoney;
@end
@implementation LEAlipayView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)init{
    if (self = [super init]) {
        self.isMove = NO;
         [self.segment setSelectedSegmentIndex:0];
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.hidden = YES;
        [self setBackgroundColor:HEXColor(0x000000, 0)];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
        [self addGestureRecognizer:tap];
        
        [self.bgView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ViewTaped)]];
       
        [self addSubview:self.bgView];
        [_bgView addSubview:self.segment];
        [_bgView addSubview:self.text1];
        [_bgView addSubview:self.text2];
        [_bgView addSubview:self.textField];
        [_bgView addSubview:self.okBtn];
        [_bgView addSubview:self.cancelBtn];
        [_bgView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_bottom).offset(GGRealValue(10));
            make.centerX.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
        }];
        [_segment makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(GGRealValue(5));
            make.centerX.equalTo(_bgView);
            make.size.equalTo(CGSizeMake(GGRealValue(60), GGRealValue(25)));
        }];
        [_textField makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_bgView);
            make.size.equalTo(CGSizeMake(GGRealValue(60), GGRealValue(20)));
        }];
        [_text1 makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_textField);
            make.right.equalTo(_textField.mas_left).offset(-GGRealValue(2));
        }];
        [_text2 makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_textField);
            make.left.equalTo(_textField.mas_right).offset(GGRealValue(2));
        }];
        [_okBtn makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView).offset(-GGRealValue(10));
            make.left.equalTo(_bgView).offset(GGRealValue(20));
            make.size.equalTo(CGSizeMake(GGRealValue(50), GGRealValue(28)));
        }];
        
        [_cancelBtn makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView).offset(-GGRealValue(10));
            make.right.equalTo(_bgView).offset(-GGRealValue(20));
            make.size.equalTo(CGSizeMake(GGRealValue(50), GGRealValue(28)));
        }];
        
    }
    return self;
}

-(void)changeNextBtnLayout:(NSNotification *)notification{
    self.isMove = YES;
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        [_bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
            make.top.equalTo(self).offset(-GGRealValue(20));
        }];
        [self layoutIfNeeded];
    }];
}
- (void) reNextBtnLayout:(NSNotification *)notification{
    
    self.isMove = NO;
    [UIView animateWithDuration:0.3 animations:^{
        [_bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
        }];
        [self layoutIfNeeded];
    }];
}

- (void)ViewTaped{
    [self.bgView endEditing:YES];
}
-(void)showLEAlipayViewWithMoney:(long)money andAliMoney:(long)AliMoney{
    self.hidden = NO;
    self.money = money;
    self.aliMoney = AliMoney;
    self.textField.text = [NSString stringWithFormat:@"%ld",money];
    [self.segment setSelectedSegmentIndex:0];
    self.text1.text = @"你想存入";
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeNextBtnLayout:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reNextBtnLayout:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.48 initialSpringVelocity:0.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
       [self.bgView remakeConstraints:^(MASConstraintMaker *make) {
           make.center.equalTo(self);
           make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
       }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}
-(void)dismissView{
    if (self.isMove == YES) {
        [self ViewTaped];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            [self.bgView remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_bottom).offset(GGRealValue(10));
                make.centerX.equalTo(self);
                make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(150)));
            }];
            [self layoutIfNeeded];
        }completion:^(BOOL finished) {
            self.hidden = YES;
             [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
             [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
            self.disBlock();
        }];
    }
}
-(void)okBtnClick{
    if (self.isMove == YES) {
        [self ViewTaped];
    }else{
        [self.delegate alipeyViewClickWithType:(int)_segment.selectedSegmentIndex money:self.textField.text];
        [self dismissView];
    }
}

-(void)cancelBtnClick{
    [self dismissView];
}
-(void)typeSegmentClick:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
        self.text1.text = @"你想存入";
        self.textField.text = [NSString stringWithFormat:@"%ld",self.money];
    }else if (segment.selectedSegmentIndex == 1){
        self.text1.text = @"你想取出";
        self.textField.text = [NSString stringWithFormat:@"%ld",self.aliMoney];
    }
}
-(UISegmentedControl *)segment{
    if (!_segment) {
        NSArray *array = @[@"存款",@"取款"];
        _segment = [[UISegmentedControl alloc] initWithItems:array];
        _segment.tintColor = HEXColor(0x000000, 1);
        [_segment addTarget:self action:@selector(typeSegmentClick:)
               forControlEvents:UIControlEventValueChanged];
    }
    return _segment;
}
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        [_bgView setBackgroundColor:HEXColor(0xffffff, 1)];
        _bgView.layer.borderColor = HEXColor(0x000000, 1).CGColor;
        _bgView.layer.borderWidth = 1;
        _bgView.layer.cornerRadius = 8;
    }
    return _bgView;
}
-(UILabel *)text1{
    if (!_text1) {
        _text1 = [[UILabel alloc] initWithTitle:@"你想存入" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(14)];
        _text1.textAlignment = NSTextAlignmentRight;
    }
    return _text1;
}

-(UILabel *)text2{
    if (!_text2) {
        _text2 = [[UILabel alloc] initWithTitle:@"元钱？" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(14)];
        _text2.textAlignment = NSTextAlignmentLeft;
    }
    return _text2;
}

-(UITextField *)textField{
    if (!_textField) {
        _textField = [UITextField new];
        _textField.borderStyle = UITextBorderStyleLine;
        _textField.backgroundColor = [UIColor clearColor];
        _textField.textColor = HEXColor(0x000000, 1);
        _textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _textField.clearButtonMode = UITextFieldViewModeNever;
        _textField.tintColor = HEXColor(0x000000, 1);
        _textField.font = [UIFont systemFontOfSize:GGRealValue(14)];
    }
    return _textField;
}

-(UIButton *)okBtn{
    if (!_okBtn) {
        _okBtn = [UIButton new];
        [_okBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_okBtn setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
        [_okBtn setTitle:@"好的" forState:UIControlStateNormal];
        [_okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}

-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton new];
        [_cancelBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_cancelBtn setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"算了" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}


@end
